﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExtBusiness.OnLine;
using System.Data;
using ExtBusiness;
using System.Web.Security;
using ExtBusiness.FlowOperation;
using System.Drawing;

public partial class office_MyHandled :PageBase
{
    private RequestFlowOperation rfoGloab = new RequestFlowOperation();

    protected void Page_Load(object sender, EventArgs e)
    {
        ReturnFromComplete();

        if (!Page.IsPostBack)
        {
            BindFlowName();
            BindMyHandled();

            if (Request["flag"] != null)//按用户ID查询
            {
                labShzt.Visible = true;
                ddlShzt.Visible = true;
            }
        }
    }

    public override void VerifyRenderingInServerForm(Control control)
    {
    }

    public void ReturnFromComplete()
    {
        if (Request["status"] == "ok")
        {
            if (Request["opera"] == "-1")
            {
                string strIID = Request["iid"];
                RequestFlowOperation rfoTemp = new RequestFlowOperation();
                rfoTemp.ReturnTaskFromComplete(strIID);
            }
        }
        else
        {
            gvMessage.Columns[gvMessage.Columns.Count - 1].Visible = false;
        }
    }

    /// <summary>
    /// 获取数据源
    /// </summary>
    /// <returns></returns>
    public DataTable GetDataSource()
    {
        //拓展表业务状态(0:已受理;2:已办结;-1:返回补证;-2:驳回中止;-3:不予受理;-4:审批不通过)
        DataTable dtTemp = new DataTable();

        string strWhere = string.Empty;
        if (!string.IsNullOrEmpty(txtTm.Text))//图名
            strWhere += string.Format(" and d.图名 like '%{0}%'", txtTm.Text);
        if (!string.IsNullOrEmpty(txtSth.Text))//审图号
            strWhere += string.Format(" and d.批准号 like '%{0}%'", txtSth.Text);
        if (!string.IsNullOrEmpty(txtShr.Text))//审核人
            strWhere += string.Format(" and exists (select 编号 from sv_work_item_list where 编号=a.iid and 经办人 like '%{0}%')", txtShr.Text);


        if (Request["flag"] != null)//按用户ID查询
        {
            if (!string.IsNullOrEmpty(txtIID.Text))//受理编号
                strWhere += string.Format(" and slbh like '%{0}%'", txtIID.Text.Trim());
            if (!string.IsNullOrEmpty(txtName.Text))//申请单位
                strWhere += string.Format(" and m.申请单位 like '%{0}%'", txtName.Text.Trim());
            if (!string.IsNullOrEmpty(ddlFlow.SelectedItem.Value))//流程名称
                strWhere += string.Format(" and m.案件类型 = '{0}'", ddlFlow.SelectedItem.Value);
            if (!string.IsNullOrEmpty(startDate.Value))//开始时间
                strWhere += string.Format(" and m.接收时间 >to_date('{0}','yyyy-mm-dd')", startDate.Value);
            if (!string.IsNullOrEmpty(endDate.Value))//结束时间
                strWhere += string.Format(" and m.接收时间 < to_date('{0}','yyyy-mm-dd')", endDate.Value);

            if (!string.IsNullOrEmpty(clBjsjStart.Value))//开始时间
                strWhere += string.Format(" and finish_time >to_date('{0}','yyyy-mm-dd')", clBjsjStart.Value);
            if (!string.IsNullOrEmpty(clBjsjEnd.Value))//结束时间
                strWhere += string.Format(" and finish_time < to_date('{0}','yyyy-mm-dd')", clBjsjEnd.Value);

            if (!string.IsNullOrEmpty(ddlTjlb.Text))//图件用途
                strWhere += string.Format(" and m.dtyt='{0}'", ddlTjlb.Text);
            if (!string.IsNullOrEmpty(ddlShzt.SelectedValue))//审核状态
                strWhere += string.Format(" and exists (select  t.submitflag from xt_submit_info t where t.iid = m.顺序号 and submitflag='{0}')", ddlShzt.SelectedValue);

            dtTemp = rfoGloab.GetMyHandledByUserID(UserId, strWhere);
        }
        else  //按业务状态查询
        {
            if (!string.IsNullOrEmpty(txtIID.Text))//受理编号
                strWhere += string.Format(" and exists (select t.slbh from ut_地图审核受理通知书 t where t.iid = a.iid and input_index = 0 and rownum < 2 and slbh like '%{0}%')", txtIID.Text.Trim());
            if (!string.IsNullOrEmpty(txtName.Text))//申请单位
                strWhere += string.Format(" and b.name like '%{0}%'", txtName.Text.Trim());
            if (!string.IsNullOrEmpty(ddlFlow.SelectedItem.Value))//流程名称
                strWhere += string.Format(" and c.wname = '{0}'", ddlFlow.SelectedItem.Value);
            if (!string.IsNullOrEmpty(startDate.Value))//开始时间
                strWhere += string.Format(" and b.accepted_time >to_date('{0}','yyyy-mm-dd')", startDate.Value);
            if (!string.IsNullOrEmpty(endDate.Value))//结束时间
                strWhere += string.Format(" and b.accepted_time < to_date('{0}','yyyy-mm-dd')", endDate.Value);

            if (!string.IsNullOrEmpty(clBjsjStart.Value))//开始时间
                strWhere += string.Format(" and b.finish_time >to_date('{0}','yyyy-mm-dd')", clBjsjStart.Value);
            if (!string.IsNullOrEmpty(clBjsjEnd.Value))//结束时间
                strWhere += string.Format(" and b.finish_time < to_date('{0}','yyyy-mm-dd')", clBjsjEnd.Value);

            if (!string.IsNullOrEmpty(ddlTjlb.Text))//图件用途
                strWhere += string.Format(" and dtyt ='{0}'", ddlTjlb.Text);
            dtTemp = rfoGloab.GetMyHandledByStatus(Request["status"],strWhere);
        }
        return dtTemp;
    }

    /// <summary>
    /// 绑定留言咨询信息
    /// </summary>
    protected void BindMyHandled()
    {
        //拓展表业务状态(0:已受理;2:已办结;-1:返回补证;-2:驳回中止;-3:不予受理;-4:审批不通过)
        DataTable dtTemp = GetDataSource();
        if (Request["flag"] != null)//按用户ID查询
        {
            gvMessage.Columns[9].Visible = true;//受理岗位
        }
        else  //按业务状态查询
        {
            gvMessage.Columns[9].Visible = false;//受理岗位
        }
        this.gvMessage.DataSource = dtTemp;
        this.gvMessage.RecordCount = dtTemp.Rows.Count;
        this.gvMessage.DataBind();
    }

    /// <summary>
    /// datagrid数据加载
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gvMessage_OnLoadData(object sender, EventArgs e)
    {
        this.BindMyHandled();
    }

    protected void btnRefer_Click(object sender, EventArgs e)
    {
        BindMyHandled();
    }

    /// <summary>
    /// 绑定该用户有权限的流程名称
    /// </summary>
    /// <!--addby zhongjian 20100608-->
    public void BindFlowName()
    {
        ClsWorkFlow newWorkflow = new ClsWorkFlow();
        DataTable dtTable = newWorkflow.GetWorkFlowByUserId(UserId, "");
        ddlFlow.Items.Clear();
        ddlFlow.Items.Add(new ListItem("所有流程", ""));
        foreach (DataRow row in dtTable.Rows)
        {
            ddlFlow.Items.Add(new ListItem(row["wname"].ToString(), row["wname"].ToString()));
        }
    }

    protected void gvMessage_OnImportClick(object sender, EventArgs e)
    {
        DataTable dtTemp = GetDataSource();
        if (dtTemp.Rows.Count > 0)
        {
            dtTemp.Columns["iid"].ColumnName = "办件编号";
            dtTemp.Columns["wname"].ColumnName = "办件类型";
            dtTemp.Columns["SCBH"].ColumnName = "批准号";
            if (dtTemp.Columns.Contains("dtyt"))
            {
                dtTemp.Columns["dtyt"].ColumnName = "图件类型";
            }
            dtTemp.Columns["tm"].ColumnName = "图名";
            dtTemp.Columns["slh"].ColumnName = "受理编号";
            dtTemp.Columns["name"].ColumnName = "申请单位";
            dtTemp.Columns["accepted_time"].ColumnName = "接件时间";
            dtTemp.Columns["step"].ColumnName = "当前岗位";
            dtTemp.Columns["PRIORITY"].ColumnName = "优先级";

            dtTemp.Columns["finish_time"].ColumnName = "流程办结时间";
            dtTemp.Columns["qfrq"].ColumnName = "签批日期";

            if (dtTemp.Columns.Contains("SQDW"))
            {
                dtTemp.Columns.Remove("SQDW");
            }
            if (dtTemp.Columns.Contains("SUBMIT_TIME"))
            {
                dtTemp.Columns.Remove("SUBMIT_TIME");
            }
            if (dtTemp.Columns.Contains("STATUS"))
            {
                dtTemp.Columns.Remove("STATUS");
            }
            string strFileName = string.Format("{0}.xls", Guid.NewGuid());
            Common.ExcelOperation.TableToExcelCustom(dtTemp, strFileName);
        }
    }
}
